package com.seegw.shaphar;

import com.alibaba.fastjson.JSON;
import com.javaweb.admin.entity.*;
import com.javaweb.admin.mapper.GSPMapper;
import com.javaweb.admin.mapper.KDLMapper;
import com.javaweb.admin.service.INcServiceLogService;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.DynamicDataSource;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.seegw.shaphar.gspreport.NCReportData;
import com.seegw.shaphar.gspreport.ReportDataBodys;
import com.sphchina.esb.webservice.ISendToReviewLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NCServiceImpl implements INCService {
    private static final Logger log = LoggerFactory.getLogger(KDLServiceImpl.class);
    @Autowired
    INcServiceLogService ncServiceLogService;
    @Autowired
    private GSPMapper GSPMapper;
    @Autowired
    private ISendToReviewLog sendToReviewLog;
    @Autowired
    private KDLMapper kdlMapper;

    /**
     * 推送GSP药品验收记录至NC
     *
     * @param id
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspPOCheck(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        //读取药品验收视图
        List<POCheck> poChecks = GSPMapper.getList();
        List<ReportDataBodys> list = new ArrayList<>();

        ReportDataBodys bodys = new ReportDataBodys();


        for (POCheck poCheck : poChecks) {
            bodys.setZdyx22("药品验收记录");
            bodys.setZdyx3(poCheck.getGrdate());
            bodys.setZdyx2(poCheck.getPkPocheck());
            bodys.setZdyx4(poCheck.getRecdate());
            bodys.setZdyx5(poCheck.getMaterialcode());
            bodys.setZdyx6(poCheck.getSuppliercode());
            bodys.setZdyx7(poCheck.getVlotno());
            bodys.setZdyx8(poCheck.getVlotnoDprodate());
            bodys.setZdyx9(poCheck.getVlotnoVinvaliddate());
            //到货数量
            //bodys.setZdyx10(((Float) (Float.parseFloat(poCheck.getQapassqty()) + Float.parseFloat(poCheck.getQarejectqty()))).toString());
            bodys.setZdyx10(poCheck.getQapassqty());
            bodys.setZdyx11(poCheck.getQapassqty());
            bodys.setZdyx12(poCheck.getQarejectqty());
            bodys.setZdyx13(poCheck.getInvoiceno());
            bodys.setZdyx14(poCheck.getInspectionresult());
            bodys.setZdyx15(poCheck.getRejectresult());
            bodys.setZdyx16(poCheck.getTreatmentforreject());
            bodys.setZdyx17(poCheck.getInspectionidname1());
            bodys.setZdyx18(poCheck.getInspectionidname2());
            bodys.setZdyx19(poCheck.getInspectionlot());
            bodys.setZdyx20(poCheck.getPlant()+poCheck.getCwarehouseid());
            //件数
            bodys.setZdyx21(poCheck.getBoxqty());
            bodys.setPk_org(poCheck.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();

            reportData.setData(list);
            body = JSON.toJSONString(reportData);

            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_POCheck", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");
                GSPMapper.updateStatus(poCheck.getPkPocheck());
            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspPurchaseReturn(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        //读取药品验收视图
        List<PurchaseReturn> purchaseReturns = GSPMapper.getPurchaseReturnList();
        List<ReportDataBodys> list = new ArrayList<>();

        ReportDataBodys bodys = new ReportDataBodys();


        for (PurchaseReturn purchaseReturn : purchaseReturns) {
            bodys.setZdyx22("药品购进退货记录");
            bodys.setZdyx3(purchaseReturn.getDbilldate());
            bodys.setZdyx4(purchaseReturn.getCkbilldate());
            bodys.setZdyx5(purchaseReturn.getMaterialcode());
            bodys.setZdyx6(purchaseReturn.getSuppname());
            bodys.setZdyx7(purchaseReturn.getVlotno());
            bodys.setZdyx8(purchaseReturn.getDprodate());
            bodys.setZdyx9(purchaseReturn.getVinvaliddate());
            //到货数量
            bodys.setZdyx10(purchaseReturn.getCkNum());

            bodys.setZdyx15(purchaseReturn.getRtReasons());

            bodys.setZdyx17(purchaseReturn.getBillmaker());

            bodys.setZdyx19(purchaseReturn.getVbillcode());

            bodys.setPk_org(purchaseReturn.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_PurchaseReturn", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");

                GSPMapper.updatePurchaseReturenStatus();

            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspOutCheck(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        //读取药品验收视图
        List<OutCheck> outChecks = GSPMapper.getOutCheckList();
        List<ReportDataBodys> list = new ArrayList<>();

        ReportDataBodys bodys = new ReportDataBodys();


        for (OutCheck outCheck : outChecks) {
            bodys.setZdyx22("药品出库复核");
            bodys.setZdyx2(outCheck.getCompanycode());
            bodys.setZdyx3(outCheck.getGidate());
            bodys.setZdyx2(outCheck.getInspectionlot());
            bodys.setZdyx5(outCheck.getMaterialcode());
            bodys.setZdyx6(outCheck.getCustomercode());
            bodys.setZdyx7(outCheck.getVendorbatch());
            bodys.setZdyx8(outCheck.getManufacturedate());
            bodys.setZdyx9(outCheck.getExpireddate());
            //到货数量
            bodys.setZdyx10(outCheck.getGiquantity());

            bodys.setZdyx14(outCheck.getInspectionresult());

            bodys.setZdyx17(outCheck.getInspectionidname1());
            bodys.setZdyx18(outCheck.getInspectionidname2());
            bodys.setZdyx19(outCheck.getInspectionlot());
            bodys.setZdyx20(outCheck.getPlant()+ outCheck.getCwarehouseid());
            bodys.setZdyx23(outCheck.getInspectiondate1());
            bodys.setZdyx24(outCheck.getInspectiondate2());
            //件数
            bodys.setZdyx21(outCheck.getPcs());
            bodys.setDr("0");
            bodys.setPk_org(outCheck.getCompanycode());
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            System.out.println(body);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_OutCheck", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");

                GSPMapper.updateOutCheckStatus(outCheck.getInspectionlot());

            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspSaleReturnCheck(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        //读取药品验收视图
        List<SaleReturnCheck> saleReturnChecks = GSPMapper.getSaleReturnCheckList();
        List<ReportDataBodys> list = new ArrayList<>();
        ReportDataBodys bodys = new ReportDataBodys();
        for (SaleReturnCheck saleReturnCheck : saleReturnChecks) {
            bodys.setZdyx22("退货药品验收");
            bodys.setZdyx3(saleReturnCheck.getGrdate());
            bodys.setZdyx2(saleReturnCheck.getPkSrcheck());
            bodys.setZdyx5(saleReturnCheck.getMaterialcode());
            bodys.setZdyx6(saleReturnCheck.getCustcode());
            bodys.setZdyx7(saleReturnCheck.getVlotno());
            bodys.setZdyx8(saleReturnCheck.getDprodate());
            bodys.setZdyx9(saleReturnCheck.getVinvaliddate());
            bodys.setZdyx10(saleReturnCheck.getNnum());
            bodys.setZdyx11(saleReturnCheck.getQapassqty());
            bodys.setZdyx12(saleReturnCheck.getQarejectqty());
            bodys.setZdyx14(saleReturnCheck.getInspectionresult());
            bodys.setZdyx15(saleReturnCheck.getRejectresult());
            bodys.setZdyx16(saleReturnCheck.getDisposalMeasures());
            bodys.setZdyx17(saleReturnCheck.getInspectionidname1());
            bodys.setZdyx18(saleReturnCheck.getInspectionidname2());
            bodys.setZdyx19("");
            bodys.setZdyx20(saleReturnCheck.getGrplant()+saleReturnCheck.getStorename());
            bodys.setZdyx21(saleReturnCheck.getNumber());
            bodys.setPk_org(saleReturnCheck.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            System.out.println(body);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_SaleReturn", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");
                GSPMapper.updateSrcheckStatus(saleReturnCheck.getPkSrcheck());
            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspRgr(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        //读取药品验收视图
        List<Rgr> rgrs = GSPMapper.getRgrList();
        List<ReportDataBodys> list = new ArrayList<>();
        ReportDataBodys bodys = new ReportDataBodys();
        for (Rgr rgr : rgrs) {
            bodys.setZdyx22("药品拒收记录");
            bodys.setZdyx3(rgr.getRgrdate());
            bodys.setZdyx2(rgr.getPkRgr());
            bodys.setZdyx5(rgr.getMaterialcode());
            bodys.setZdyx6(rgr.getSuppliercode());
            bodys.setZdyx7(rgr.getVendorbatch());
            bodys.setZdyx8(rgr.getDprodate());
            bodys.setZdyx9(rgr.getVinvaliddate());
            bodys.setZdyx12(rgr.getRgrquantity());
            bodys.setZdyx15(rgr.getRgrreason());
            bodys.setZdyx17(rgr.getRgrnamr());
            bodys.setZdyx19(rgr.getVendorpo());
            bodys.setPk_org(rgr.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            System.out.println(body);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_RGR", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");
                GSPMapper.updateRgrStatus(rgr.getPkRgr());
            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspPurchasewe(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        //读取药品验收视图
        List<Purchasewe> purchasewes = GSPMapper.getPurchaseweList();
        List<ReportDataBodys> list = new ArrayList<>();
        ReportDataBodys bodys = new ReportDataBodys();
        for (Purchasewe purchasewe : purchasewes) {
            bodys.setZdyx22("药品收货记录");
            bodys.setZdyx3(purchasewe.getGrdate());
            bodys.setZdyx2(purchasewe.getPkWe());
            bodys.setZdyx5(purchasewe.getMaterialcode());
            bodys.setZdyx6(purchasewe.getVendorcode());
            bodys.setZdyx7(purchasewe.getVendorbatch());
            bodys.setZdyx10(purchasewe.getGrquantity());
            bodys.setZdyx11(purchasewe.getRequantity());
            bodys.setZdyx12(purchasewe.getRgrquantity());

            bodys.setZdyx15(purchasewe.getRgrreason());
            bodys.setZdyx17(purchasewe.getGridname());
            bodys.setZdyx18(purchasewe.getRgrnamr());
            bodys.setZdyx21(purchasewe.getPcs());

            bodys.setPk_org(purchasewe.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            System.out.println(body);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_Purchasewe", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");
                GSPMapper.updatePurchaseweStatus(purchasewe.getPkWe());
            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitKdlMaintainReport(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        List<MaintainReport> maintainReports = kdlMapper.getMaintaiReportList();
        List<ReportDataBodys> list = new ArrayList<>();
        ReportDataBodys bodys = new ReportDataBodys();
        for (MaintainReport maintainReport : maintainReports) {
            bodys.setZdyx22("养护记录");
            bodys.setPk_org("");
            bodys.setZdyx3(maintainReport.getVdatum());
            bodys.setZdyx2(maintainReport.getInspectionlot());
            bodys.setZdyx5(maintainReport.getMaterialcode());
            bodys.setZdyx7(maintainReport.getVendorbatch());
            bodys.setZdyx8(maintainReport.getManufacturedate());
            bodys.setZdyx9(maintainReport.getExpireddate());
            bodys.setZdyx10(maintainReport.getLosmenge());
            bodys.setZdyx13(maintainReport.getMaterialspec());
            bodys.setZdyx17(maintainReport.getVname());
            bodys.setZdyx20("");
            bodys.setZdyx21(maintainReport.getPcs());
            bodys.setZdyx30("30天");
            bodys.setZdyx31("");
            bodys.setZdyx32("");
//
//            bodys.setPk_org(maintainReport.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            System.out.println(body);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_MaintainReport", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");
                kdlMapper.updateMaintaiReportStatus(maintainReport.getInspectionlot());
            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspShipRecord(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        List<TransReport> transReports = kdlMapper.getTransReportList();
        List<ReportDataBodys> list = new ArrayList<>();
        ReportDataBodys bodys = new ReportDataBodys();
        for (TransReport transReport : transReports) {
            bodys.setZdyx22("药品委托运输");
            bodys.setPk_org(transReport.getCompanycode());
            bodys.setZdyx3(transReport.getDispatchDate());
            bodys.setZdyx2(transReport.getPkTrans());
            bodys.setZdyx5(transReport.getZzprnpn());
            bodys.setZdyx6(transReport.getCustcode());
            bodys.setZdyx17(transReport.getCheckname1());
            bodys.setZdyx19(transReport.getPackingListNumber());
            bodys.setZdyx21(transReport.getNoOfParcel());
            bodys.setZdyx25(transReport.getDeliveryAddress());
            bodys.setZdyx26(transReport.getServiceVendor());
            bodys.setZdyx27(transReport.getShpmntTool());
            bodys.setZdyx28(transReport.getPlateNo());
            bodys.setZdyx29(transReport.getDeliveryPerson());
//
//            bodys.setPk_org(maintainReport.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            System.out.println(body);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_TransReport", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");
                kdlMapper.updateTransReportStatus(transReport.getPkTrans(),transReport.getMaterialtype());
            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspShipRecordSpecial(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        List<TransReport> transReports = kdlMapper.getTransReportListSpecial();
        List<ReportDataBodys> list = new ArrayList<>();
        ReportDataBodys bodys = new ReportDataBodys();
        for (TransReport transReport : transReports) {
            bodys.setZdyx22("药品委托运输(特药)");
            bodys.setPk_org(transReport.getCompanycode());
            bodys.setZdyx3(transReport.getDispatchDate());
            bodys.setZdyx2(transReport.getPkTrans());
            bodys.setZdyx5(transReport.getZzprnpn());
            bodys.setZdyx6(transReport.getCustcode());
            bodys.setZdyx17(transReport.getCheckname1());
            bodys.setZdyx18(transReport.getCheckname2());
            bodys.setZdyx19(transReport.getPackingListNumber());
            bodys.setZdyx21(transReport.getNoOfParcel());
            bodys.setZdyx25(transReport.getDeliveryAddress());
            bodys.setZdyx26(transReport.getServiceVendor());
            bodys.setZdyx27(transReport.getShpmntTool());
            bodys.setZdyx28(transReport.getPlateNo());
            bodys.setZdyx29(transReport.getDeliveryPerson());
//
//            bodys.setPk_org(maintainReport.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            System.out.println(body);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_TransReport", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");
                kdlMapper.updateTransReportStatus(transReport.getPkTrans(),transReport.getMaterialtype());
            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());

    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult submitGspUnQualifiedRecord(Integer id) {
        String body = "";
        StringBuilder sb = new StringBuilder();
        NCReportData reportData = new NCReportData();
        reportData.setInitid("Report");
        List<GspUnQualified> transReports = kdlMapper.getGspUnQualifiedList();
        List<ReportDataBodys> list = new ArrayList<>();
        ReportDataBodys bodys = new ReportDataBodys();
        for (GspUnQualified transReport : transReports) {
            bodys.setZdyx22("不合格药品记录");
            bodys.setPk_org(transReport.getCompanycode());
            bodys.setZdyx3(transReport.getInspectiondate1());
            bodys.setZdyx2(transReport.getPkGsp());
            bodys.setZdyx4(StringUtils.trim(transReport.getOperTime()));
            bodys.setZdyx5(transReport.getCmaterialvid());
            bodys.setZdyx6(transReport.getManufacturername());
            bodys.setZdyx7(transReport.getVlotno());
            bodys.setZdyx9(transReport.getExpireddate());
            bodys.setZdyx10(transReport.getQty().toString());
            bodys.setZdyx15(StringUtils.trim(transReport.getTreatmentforreject()));
            bodys.setZdyx16(StringUtils.trim(transReport.getFeedback()));
            bodys.setZdyx17(StringUtils.trim(transReport.getOperUser()));
            bodys.setZdyx19(StringUtils.trim(transReport.getOrderno()));
            bodys.setZdyx30(StringUtils.trim(transReport.getSource()));
            bodys.setZdyx31(StringUtils.trim(transReport.getSuppliername()));
//
//            bodys.setPk_org(maintainReport.getCompanycode());
            bodys.setDr("0");
            list.add(bodys);
            bodys = new ReportDataBodys();
            reportData.setData(list);
            body = JSON.toJSONString(reportData);
            System.out.println(body);
            JsonResult result = sendToReviewLog.callEsbKdl(reportData.getInitid()+"_Unqualified", "", body);
            if (result.getCode() > 0) {
                sb.append(result.getMsg());
            } else {
                //更新回写状态
                DynamicDataSource.setDataSource("sinesys");
                kdlMapper.updateUnqualifiedStatus(transReport.getPkGsp());
            }
            list.clear();
        }

        return StringUtils.isEmpty(sb.toString()) ? JsonResult.success() : JsonResult.error(sb.toString());    }
}
